import PermissionBtn from "@/components/PermissionBtn";
import { Button, Modal, Pagination, Space, Table, TableProps,Image } from "antd";
import PhotoSearchForm from "./PhotoSearchForm";
import { usePhotoAlbum } from "../hooks/usePhotoAlbum";
import { ExclamationCircleFilled, PlusOutlined } from "@ant-design/icons";
import { usePhotoAlbumOption } from "../hooks/usePhotoAlbumOption";
import { Attachment } from "@/types/content/attachment";
import PhotoAlbumOption from "./PhotoAlbumOption";

export default function PhotoAlbum() {
  const {loading,tableData,pagination,setPagination,getTableList,removePhotoAlbum,setSearchForm} = usePhotoAlbum()
  const {isModalOpen,openOptionDialog,oldInfo,optionType,close} = usePhotoAlbumOption()
  const showConfirm = (row: Attachment.Row) => {
    Modal.confirm({
      title: "警告",
      icon: <ExclamationCircleFilled />,
      content: `确认删除?`,
      okText: "确认",
      cancelText: "取消",
      okType: "danger",
      onOk() {
        removePhotoAlbum(row);
      },
    });
  };
  const columns: TableProps<Attachment.Row>["columns"] = [
    {
      title: "图片",
      dataIndex: "url",
      key: "url",
      width: 100,
      align: "center",
      render: (_, { url }) => (
        <>{url && <Image width={100} height={100} src={url} />}</>
      ),
    },
    {
      title: "标签",
      key: "tag",
      dataIndex: "tag",
      ellipsis: true,
      width: 100,
      align: "center",
    },
    {
      title: "上传时间",
      key: "createTime",
      dataIndex: "createTime",
      ellipsis: true,
      width: 100,
      align: "center",
    },
    
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 50,
      align: "center",
      render: (_, row) => (
        <Space size={2} wrap>
          <PermissionBtn code="love.removeFile">
            <Button
              type="link"
              danger
              onClick={() => {
                showConfirm(row);
              }}
            >
              删除
            </Button>
          </PermissionBtn>
        </Space>
      ),
    },
  ];
  return (
    <div className="page__wrap--g" style={{ padding: 0 }}>
      <div className="form__wrap--g">
        <PhotoSearchForm
          onChange={(form: Attachment.QueryForm) => {
            setSearchForm(form);
          }}
        />
        <PermissionBtn code="love.addFile">
          <Button
            type="primary"
            icon={<PlusOutlined />}
            size="middle"
            onClick={() => {
              openOptionDialog("add");
            }}
          >
            新增
          </Button>
        </PermissionBtn>
      </div>
      <div className="table__wrap--g">
        <Table
          loading={loading}
          columns={columns}
          dataSource={tableData}
          rowKey="id"
          sticky
          bordered
          size="small"
          scroll={{ x: 1300 }}
          pagination={false}
        />
      </div>
      <div className="pagination__wrap--g">
        <Pagination
          pageSize={pagination.pageSize}
          current={pagination.pageNo}
          total={pagination.total}
          showQuickJumper={true}
          showTotal={(total) => `共${total}条`}
          onChange={(page, size) => {
            setPagination({
              total: pagination.total,
              pageNo: page,
              pageSize: size,
            });
          }}
        />
      </div>

      {isModalOpen && (
        <PhotoAlbumOption
          isModalOpen={isModalOpen}
          optionType={optionType}
          oldInfo={oldInfo}
          close={close}
          change={() => {
            getTableList();
          }}
        />
      )}
    </div>
  );
}
